package com.banzhi.shop.activity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.design.widget.TabLayout;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.R;
import com.banzhi.shop.adapter.GuigeAdapter3;
import com.banzhi.shop.adapter.Guigeadapter;
import com.banzhi.shop.adapter.MyPagerAdapter;
import com.banzhi.shop.base.BaseActivity;
import com.banzhi.shop.entity.GoodsDetails.Buy_Data;
import com.banzhi.shop.entity.InviteShareSuCaimatterInfo.InviteShareSuCaiAdapter;
import com.banzhi.shop.entity.InviteShareSuCaimatterInfo.InviteShareSuCaiCallBack;
import com.banzhi.shop.entity.InviteShareSuCaimatterInfo.InviteShareSuCaiImpl;
import com.banzhi.shop.entity.InviteShareSuCaimatterInfo.InviteShareSuCai_Data;
import com.banzhi.shop.entity.InviteShopkeeperShareDetails.GoodsShareDetails;
import com.banzhi.shop.entity.InviteShopkeeperShareDetails.InviteShopKeeperShareDetailsImpl;
import com.banzhi.shop.entity.WXshare;
import com.banzhi.shop.model.CallBack.CommoditySucaiCallBack;
import com.banzhi.shop.model.CallBack.InviteShopKeeperDetailsCallBack;
import com.banzhi.shop.model.CallBack.WXshareCallBack;
import com.banzhi.shop.model.ICommoditySucaiModel;
import com.banzhi.shop.model.Impl.CommoditySucaiImpl;
import com.banzhi.shop.model.Impl.WXshareImpl;
import com.banzhi.shop.model.WXshareModel;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.NumberUtils;
import com.banzhi.shop.utils.ShareUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.utils.Translucent;
import com.banzhi.shop.utils.XutilImage;
import com.banzhi.shop.view.CustomSwipeToRefresh;
import com.squareup.picasso.Picasso;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.sharesdk.framework.ShareSDK;

import static com.banzhi.shop.R.id.btn_sucai3;
import static com.banzhi.shop.R.id.btn_xiangqing3;
import static com.banzhi.shop.R.id.ll_xiangqing3;

public class BecomeShoperDetailsActivity  extends BaseActivity implements View.OnClickListener, WXshareCallBack,InviteShareSuCaiCallBack, InviteShareSuCaiAdapter.InviteShareCallBack, SwipeRefreshLayout.OnRefreshListener, com.banzhi.shop.entity.InviteShopkeeperShareDetails.InviteShopKeeperDetailsCallBack, GuigeAdapter3.GettextCallBack, CommoditySucaiCallBack {
    private String wxURL;
    private static final int PENGYOUQUAN  = 1;
    private static final int WEIXINHAOYOU = 2;
    private static final int LIANJIE      = 3;
    private static final int ERWEIMA      = 4;
    private WXshareModel wxmodel;
    private String                           goodID;
    private InviteShopKeeperShareDetailsImpl mShareDetailsModel;

    private boolean iscroll;
    private Button mBtnSuCai3;
    private Button         btnXiangqing3;
    private ImageView      mIvBack3;
    private ImageView      mIvShare3;
    private LinearLayout mLlXiangqing3;
    //素材
    private RelativeLayout mRlSuCai3;
    /**
     * 商详情里的导航栏
     */
    private TabLayout tabLayout;
    private List<GoodsShareDetails> goodsShareDetails = new ArrayList<>();
    private ViewPager mVpgoodsdata;
    private TextView mTvTitle3;
    private TextView          mTvShareTitle3;
    private TextView          mTvMarketPrice3;
    private TextView          mTvInviteFriends;
    private ViewPager mViewPager;
    private int               width;
    private int               height;
    private TextView          mTvPicNum;
    private TextView          mTvAll3;
    private TextView          mTvLinYun3;
    //商品详情类
    private GoodsShareDetails goodsShareDetailsData;


    private int type = 1;

    /**
     * 展示图片的imageview集合
     */
    private ArrayList<ImageView> titleImages = new ArrayList<>();
    //素材实体类集合
    private List<InviteShareSuCai_Data> inviteShareSuCaiData = new ArrayList<>( );


    /**
     * 图片的URI地址集合
     */
    private List<String> mImageResIDs= new ArrayList<>();
    private WebView mWvPic;
    private InviteShareSuCaiImpl mInviteShareSuCaiModel;
    private CustomSwipeToRefresh mSrlListView3;
    private ListView mLvSuCai3;
    private RelativeLayout mRlNoData;

    private InviteShareSuCaiAdapter mAdapter;

    private int position;
    private InviteShareSuCai_Data inviteShareSuCai_Data;
    private GuigeAdapter3 guigeadapter;
    private ICommoditySucaiModel sucaiModel;
    /**
     * 选择规格里的价格
     */
    private TextView tvguigeprice;
    /**
     * 选择规格里的库存
     */

    private TextView tvguigeliyun;
    private ImageView ivguigeimage;
    private LinearLayout ll_guigekucun;
    /**
     * 默认商品数量为1
     */
    private int number = 1;
    /**
     * 存储用户点击选择的规格属性
     */
    private int[] strs;
    /**
     * 判断是不是全部选择了规格
     */
    private boolean isallcheck;
    private StringBuffer allguige = new StringBuffer();
    private String guigejiage;
    private String guigekuchun;

    private String guigelirun;
    private String guigeid = null;
    private String guigeimage;
    private String guigetitle;

    /**
     * 商品库存
     */
    private TextView tvkucun;
    /**
     * 库存
     */

    private String num;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_become_shoper_details);
        Translucent.setTranslucent( this );
        ShareSDK.initSDK(this, "1dc05ab16fd6c");
        setinits();
        setListeners();
        Intent intent = getIntent();
        goodID = intent.getStringExtra( "id" );
        //type == 1 跳到詳情頁
        String type = intent.getStringExtra( "type" );
        if ("1".equals( type )) {
            btnXiangqing3.setBackgroundResource( R.mipmap.bg4 );
            mBtnSuCai3.setBackgroundResource( R.mipmap.bg5 );
            mLlXiangqing3.setVisibility( View.GONE );
            mRlSuCai3.setVisibility( View.VISIBLE );
            mBtnSuCai3.setTextColor( ContextCompat.getColor( this, R.color.themeColor ) );
            btnXiangqing3.setTextColor( Color.WHITE);
        }
        mInviteShareSuCaiModel = new InviteShareSuCaiImpl();
        mShareDetailsModel = new InviteShopKeeperShareDetailsImpl();
        LoadingDialogUtils.showProgress( this, "正在加载,请稍后~" );
        mShareDetailsModel.getData(goodID,this);

        wxmodel = new WXshareImpl();

        sucaiModel = new CommoditySucaiImpl();
    }
    private void showWebView() {
        String content = goodsShareDetailsData.getContent();
        mWvPic.loadDataWithBaseURL( null,content,"text/html","UTF-8",null );

        mWvPic.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);//不加上，会显示白边
        WebSettings settings = mWvPic.getSettings();
        //缓存机制
        settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK );
        //设置加载进来的页面自适应手机屏幕

        LogUtils.d( "链接",content );
    }
    //xiangqing
    private void showgoodsShareDetailsData() {
        //图片展示
        mImageResIDs = goodsShareDetailsData.getThumb_url();
        mTvAll3.setText(mImageResIDs.size()+"");
        prepareData();
        MyPagerAdapter myPagerAdapter = new MyPagerAdapter(titleImages);
        // ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter( titleImages );
        mViewPager.setAdapter( myPagerAdapter );
        //展示内容
        mTvTitle3.setText( goodsShareDetailsData.getTitle() );
        mTvShareTitle3.setText( goodsShareDetailsData.getShare_title() );
        mTvMarketPrice3.setText( goodsShareDetailsData.getMarketprice() );
        if (ASUtils.isCanUse(goodsShareDetailsData.getProfit())) {
            mTvLinYun3.setText(NumberUtils.DecimalFormat(goodsShareDetailsData.getProfit()));
        }

    }
    //首页图片放置在imageview上
    private void prepareData() {
        ImageView iv;
        for (int i = 0; i < mImageResIDs.size(); i++) {
            iv = new ImageView( this );

            XutilImage.display( iv,mImageResIDs.get( i ) );
            titleImages.add( iv );
        }

    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    private void setinits() {
        mIvBack3 = (ImageView) findViewById( R.id.iv_back3 );
        btnXiangqing3 = (Button) findViewById( btn_xiangqing3 );
        mBtnSuCai3 = (Button) findViewById( btn_sucai3 );
        mIvShare3 = (ImageView) findViewById( R.id.iv_share3 );
        mLlXiangqing3 = (LinearLayout) findViewById( ll_xiangqing3 );
        mRlSuCai3 = (RelativeLayout) findViewById( R.id.rl_sucai3 );
        mViewPager = (ViewPager) findViewById( R.id.vp_title3 );
        DisplayMetrics dm = getResources().getDisplayMetrics();
        width = dm.widthPixels;
        height = width;
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT, height );
        //获取当前控件的布局对象
        //params.height=width;//设置当前控件布局的高度
        mViewPager.setLayoutParams( params );//将设置好的布局参数应用到控件中
        mTvTitle3 = (TextView) findViewById( R.id.tv_title3 );
        mTvShareTitle3 = (TextView) findViewById( R.id.tv_share_title3 );
        mTvMarketPrice3 = (TextView) findViewById( R.id.tv_marketprice3 );
        mTvInviteFriends = (TextView) findViewById( R.id.tv_inviteFriends );
        mTvPicNum = (TextView) findViewById( R.id.tv_pic_num );
        mTvAll3 = (TextView) findViewById( R.id.tv_all3 );
        mTvLinYun3 = (TextView) findViewById( R.id.tv_liyun3 );
        mWvPic = (WebView) findViewById( R.id.wv_pic );
        mSrlListView3 = (CustomSwipeToRefresh) findViewById( R.id.srl_listview3 );
        mSrlListView3.setColorSchemeResources(R.color.themeColor);
        mSrlListView3.setProgressBackgroundColorSchemeResource(R.color.colorWhite);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mLvSuCai3 = (ListView) findViewById( R.id.sucai_listview3 );
        mRlNoData= (RelativeLayout) findViewById( R.id.rl_nodata3 );
    }

    private void setListeners() {
        mIvBack3.setOnClickListener( this );
        mIvShare3.setOnClickListener( this );
        btnXiangqing3.setOnClickListener( this );
        mBtnSuCai3.setOnClickListener( this );
        mTvInviteFriends.setOnClickListener( this );
        mWvPic.setOnClickListener( this );
        mSrlListView3.setOnRefreshListener( this );
        mViewPager.addOnPageChangeListener( new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mTvPicNum.setText( position +1+"" );

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        } );

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back3:
                finish();
                break;
            case R.id.iv_share3:
                showSharePopupwindow(goodsShareDetailsData );
           /*  InviteShopkeeperShareDetailsActivity.this.position = position;*/
                break;
            case R.id.btn_xiangqing3:
                btnXiangqing3.setBackgroundResource( R.mipmap.bg1 );
                mBtnSuCai3.setBackgroundResource( R.mipmap.bg2 );
                mLlXiangqing3.setVisibility( View.VISIBLE );
                mRlSuCai3.setVisibility( View.GONE );
                btnXiangqing3.setTextColor( ContextCompat.getColor( this, R.color.themeColor ) );
                mBtnSuCai3.setTextColor( Color.WHITE );
                break;
            case R.id.btn_sucai3:
                btnXiangqing3.setBackgroundResource( R.mipmap.bg4 );
                mBtnSuCai3.setBackgroundResource( R.mipmap.bg5 );
                mLlXiangqing3.setVisibility( View.GONE );
                mRlSuCai3.setVisibility( View.VISIBLE );
                mBtnSuCai3.setTextColor( ContextCompat.getColor( this, R.color.themeColor ) );
                btnXiangqing3.setTextColor( Color.WHITE );
                break;
            case R.id.tv_inviteFriends:
            //    showSharePopupwindow(goodsShareDetailsData );
                showGuige();
            //showguigePopupwindow();
                break;

        }

    }

    private void showGuige() {
        if (type == 1) {//点击了选择规格
            showguigePopupwindow();
        }else if (type == 3) {//点击购买
            //库存量
            num = goodsShareDetailsData.getTotal()+"";
            if (ASUtils.isCanUse(num) && Integer.parseInt(num) > 0) {
                if (goodsShareDetailsData.getOptionstit() != null) {
                    if (guige != null && goodsnum != null) {
                        for (int i = 0; i < goodsShareDetailsData.getOptions().size(); i++) {
                            if (guige.equals(goodsShareDetailsData.getOptions().get(i).getSpecs())) {
                                guigeid = goodsShareDetailsData.getOptions().get(i).getId()+"";
                            }
                        }
                        LoadingDialogUtils.showProgress(this, "正在加载,请稍后~");
                        sucaiModel.buy(goodID, goodsnum, guigeid, this);
                    } else {
                        showguigePopupwindow();
                    }
                } else {
                    if (goodsnum == null) {
                        showguigePopupwindow();
                    } else {
                        LoadingDialogUtils.showProgress(this, "正在加载,请稍后~");
                        sucaiModel.buy(goodID, goodsnum, guigeid, this);
                    }
                }
            } else {
                ToastUtils.makeToastShort("商品库存为0");
            }

        }

    }
    /**
     * 用来存储商品选择后的规格
     */
    private String guige;
    /**
     * 用来选择商品选择后数量
     */
    private String goodsnum;
    /**
     * 选择规格后的显示
     */
    private TextView tvchoice;
    /**
     * 商品价格
     */
    private TextView tvprice;
    /**
     * 商品利润
     */
    private TextView tvlirun;
    private TextView tvguigekucun;
    private void showguigePopupwindow() {
        //设置contentView
        View contentView = LayoutInflater.from(BecomeShoperDetailsActivity.this).inflate(R.layout.popupwindow_choice_guige3, null);
        final PopupWindow popWindow = new PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true);
        popWindow.setContentView(contentView);
        popWindow.setAnimationStyle(R.style.AnimBottom);
        popWindow.setFocusable(true);
        popWindow.setOutsideTouchable(true);// 设置允许在外点击消失

        //设置各个控件的点击响应
        ImageView ivchacha = (ImageView) contentView.findViewById(R.id.iv_chacha);
        ImageView ivimage = (ImageView) contentView.findViewById(R.id.iv_image);
        tvguigeprice = (TextView) contentView.findViewById(R.id.tv_price);
        tvguigeliyun = (TextView) contentView.findViewById(R.id.tv_liyun);
        tvguigekucun = (TextView) contentView.findViewById(R.id.tv_guige_kucun_shoper);
        ivguigeimage = (ImageView) contentView.findViewById(R.id.iv_image);
        ll_guigekucun = (LinearLayout) contentView.findViewById(R.id.ll_guigekucun);
        final TextView tvjian = (TextView) contentView.findViewById(R.id.tv_jian);
        final TextView tvjia = (TextView) contentView.findViewById(R.id.tv_jia);
        final TextView tvnum = (TextView) contentView.findViewById(R.id.tv_num);
        Button btntrue = (Button) contentView.findViewById(R.id.btn_true);
        ListView listviewgg = (ListView) contentView.findViewById(R.id.listview_guige);

        RelativeLayout rlbackground = (RelativeLayout) contentView.findViewById(R.id.rl_background);
        LinearLayout ll = (LinearLayout) contentView.findViewById(R.id.ll);
        rlbackground.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });
        ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        List<GoodsShareDetails.OptionstitBean> optionstit = goodsShareDetailsData.getOptionstit();
        if ( goodsShareDetailsData != null) {
            guigeadapter = new GuigeAdapter3(this, optionstit);
            listviewgg.setAdapter(guigeadapter);
            if (optionstit.size() > 0) {
                //创建装规格元素的数组
                strs = new int[optionstit.size()];
            }
            guigeadapter.setCallBack(this);

            if (ASUtils.isCanUse(goodsShareDetailsData.getMarketprice())) {
                tvguigeprice.setText(NumberUtils.DecimalFormat(goodsShareDetailsData.getMarketprice()));
            }
            if (ASUtils.isCanUse(goodsShareDetailsData.getProfit())) {
                tvguigeliyun.setText(NumberUtils.DecimalFormat(goodsShareDetailsData.getProfit()));
            }
            if (ASUtils.isCanUse(goodsShareDetailsData.getThumb_url().get(0))) {
                Picasso.with(BecomeShoperDetailsActivity.this).load(goodsShareDetailsData.getThumb_url().get(0)).placeholder(R.mipmap.zhanweitu2).into(ivguigeimage);
            }
           tvguigekucun.setText(goodsShareDetailsData.getShowtotal()+"");

            if (goodsShareDetailsData.getThumb_url().size() > 0) {
                Picasso.with(this).load(goodsShareDetailsData.getThumb_url().get(0)).placeholder(R.mipmap.zhanweitu2).into(ivimage);
            }
        }
        tvnum.setText(number + "");
        if (number == 1) {
            tvjian.setEnabled(false);
        }
        tvjian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (number > 1) {
                    number--;
                } else {
                    number = 1;
                }
                if (number == 1) {
                    tvjian.setEnabled(false);
                }
                tvnum.setText(number + "");
            }
        });

        tvjia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("1".equals(goodsShareDetailsData.getShowtotal()+"") && Integer.parseInt(tvguigekucun.getText().toString().trim()) <= number) {
                    ToastUtils.makeToastShort("该规格商品库存不足");
                } else {
                    if (goodsShareDetailsData.getUsermaxbuy() != 0 && number >= goodsShareDetailsData.getUsermaxbuy() - goodsShareDetailsData.getUserbuy()) {
                        ToastUtils.makeToastShort("已超过最大限购数量");
                    } else {
                        number++;
                        tvnum.setText(number + "");
                        tvjian.setEnabled(true);
                    }
                }
            }
        });
        ivchacha.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                number = 1;
                popWindow.dismiss();
            }
        });
        btntrue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if ("1".equals(goodsShareDetailsData.getShowtotal()+"") && "0".equals(tvguigekucun.getText().toString().trim())) {
                    ToastUtils.makeToastShort("商品库存为0，请选择别的规格");
                } else {
                    if (goodsShareDetailsData.getUsermaxbuy() != 0 && number > goodsShareDetailsData.getUsermaxbuy() - goodsShareDetailsData.getUserbuy()) {
                        ToastUtils.makeToastShort("此账号已购买过" + goodsShareDetailsData.getUsermaxbuy() + "件");
                    } else {
                        if (strs != null) {
                            for (int i = 0; i < strs.length; i++) {
                                if (strs[i] == 0) {
                                    isallcheck = false;
                                    break;
                                } else {
                                    isallcheck = true;
                                    continue;
                                }
                            }
                        } else {
                            isallcheck = true;
                        }

                        if (!isallcheck) {
                            ToastUtils.makeToastShort("请选择规格");
                        } else {
                            //判断是由哪个按钮调起的选择规格 ，做不同的事情
                            goodsnum = tvnum.getText().toString().trim();
                           /* if (type == 1) {
                                if (guige != null) {
                              //      tvchoice.setText("已选:" + "数量×" + goodsnum + "  " + guigetitle);
                                } else {
                               //     tvchoice.setText("已选:" + "数量×" + goodsnum);
                                }
                                if (ASUtils.isCanUse(guigelirun)) {
                           //         tvlirun.setText(NumberUtils.DecimalFormat(guigelirun));
                                }
                            *//*    if (ASUtils.isCanUse(guigekuchun)) {
                                    tvkucun.setText(guigekuchun);
                                }*//*
                                if (ASUtils.isCanUse(guigejiage)) {
                                  //  tvprice.setText(NumberUtils.DecimalFormat(guigejiage));
                                }
                            } else if (type == 3) {
                                LoadingDialogUtils.showProgress(BecomeShoperDetailsActivity.this, "正在加载,请稍后~");
                                sucaiModel.buy(goodID, goodsnum, guigeid, BecomeShoperDetailsActivity.this);
                            }*/
                            LoadingDialogUtils.showProgress(BecomeShoperDetailsActivity.this, "正在加载,请稍后~");
                            sucaiModel.buy(goodID, goodsnum, guigeid, BecomeShoperDetailsActivity.this);
                            //值还原
                            number = 1;
                            type = 1;
                            popWindow.dismiss();
                        }
                    }

                }

            }
        });
        ColorDrawable dw = new ColorDrawable(0x70000000);
        popWindow.setBackgroundDrawable(dw);
        popWindow.showAtLocation(contentView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);

    }

    private void showSharePopupwindow(final GoodsShareDetails goodsShareDetailsData) {
        //设置contentView
        View contentView = LayoutInflater.from(this).inflate(R.layout.popupwindow_share_goods, null);
        final PopupWindow popWindow = new PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true);
        popWindow.setContentView(contentView);
        popWindow.setAnimationStyle(R.style.AnimBottom);

        popWindow.setFocusable(true);
        popWindow.setOutsideTouchable(false);// 设置允许在外点击消失
        //设置各个控件的点击响应
        ImageView ivchacha = (ImageView) contentView.findViewById(R.id.iv_chacha);
        TextView tvprice = (TextView) contentView.findViewById(R.id.tvmoney);
        TextView tvpricetwo = (TextView) contentView.findViewById(R.id.tv_money_two);
        LinearLayout llpengyouquan = (LinearLayout) contentView.findViewById(R.id.ll_pengyouquan);
        LinearLayout llweixin = (LinearLayout) contentView.findViewById(R.id.ll_weixing);
        LinearLayout lllianjie = (LinearLayout) contentView.findViewById(R.id.ll_lianjie);
        LinearLayout llerweima = (LinearLayout) contentView.findViewById(R.id.ll_erweima);
        RelativeLayout rlbackground = (RelativeLayout) contentView.findViewById(R.id.rl_background);
        LinearLayout ll = (LinearLayout) contentView.findViewById(R.id.ll);
        rlbackground.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });
        ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        if (goodsShareDetailsData != null) {
            tvprice.setText(NumberUtils.DecimalFormat( goodsShareDetailsData.getProfit()));
            tvpricetwo.setText(NumberUtils.DecimalFormat(goodsShareDetailsData.getProfit()));
            lllianjie.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (wxURL == null) {
                        LoadingDialogUtils.showProgress(BecomeShoperDetailsActivity.this, "正在加载,请稍后~");
                        wxmodel.getData(goodsShareDetailsData.getId(), null, LIANJIE, BecomeShoperDetailsActivity.this);
                    } else {
                        ShareUtils.copyLianjie(BecomeShoperDetailsActivity.this, wxURL);
                    }
                }
            });

            llerweima.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (wxURL == null) {
                        LoadingDialogUtils.showProgress(BecomeShoperDetailsActivity.this, "正在加载,请稍后~");
                        wxmodel.getData(goodsShareDetailsData.getId(), null, ERWEIMA, BecomeShoperDetailsActivity.this);
                    } else {
                        ShareUtils.ereima3(BecomeShoperDetailsActivity.this, goodsShareDetailsData.getTitle(), goodsShareDetailsData.getMarketprice(), null, null, null, goodsShareDetailsData.getThumb(),  wxURL);
                    }
                }
            });
            llweixin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (wxURL == null) {
                        LoadingDialogUtils.showProgress(BecomeShoperDetailsActivity.this, "正在加载,请稍后~");
                        wxmodel.getData(goodsShareDetailsData.getId(), null, WEIXINHAOYOU, BecomeShoperDetailsActivity.this);
                    } else {
                        //  ShareUtils.wxshare(goodsShareDetailsData.getTitle(), goodsShareDetailsData.getThumb(),  "全球十佳网商联合出品。匠心工厂，源头供货，正品优品，尽在半职。", wxURL);
                        ShareUtils.wxshare(goodsShareDetailsData.getTitle(), goodsShareDetailsData.getThumb_url().get(0),  "全球十佳网商联合出品。匠心工厂，源头供货，正品优品，尽在半职。", wxURL);
                    }
                }
            });
            llpengyouquan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (wxURL == null) {
                        LoadingDialogUtils.showProgress(BecomeShoperDetailsActivity.this, "正在加载,请稍后~");
                        wxmodel.getData(goodsShareDetailsData.getId(), null, PENGYOUQUAN, BecomeShoperDetailsActivity.this);
                    } else {
                        ShareUtils.wxPengyouquan(goodsShareDetailsData.getTitle(), goodsShareDetailsData.getThumb(), wxURL);
                    }
                }
            });
        }
        ivchacha.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                wxURL = null;
                popWindow.dismiss();
            }
        });
        ColorDrawable dw = new ColorDrawable(0x70000000);
        popWindow.setBackgroundDrawable(dw);
        popWindow.showAtLocation(contentView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);


    }


    private void showSharePopupwindow() {
        //设置contentView
        View contentView = LayoutInflater.from(BecomeShoperDetailsActivity.this).inflate(R.layout.popupwindow_share_dianzhu, null);
        final PopupWindow popWindow = new PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true);
        popWindow.setContentView(contentView);
        popWindow.setAnimationStyle(R.style.AnimBottom);

        popWindow.setFocusable(true);
        popWindow.setOutsideTouchable(false);// 设置允许在外点击消失
        //设置各个控件的点击响应
        ImageView ivchacha = (ImageView) contentView.findViewById(R.id.iv_chacha);
        LinearLayout llpengyouquan = (LinearLayout) contentView.findViewById(R.id.ll_pengyouquan);
        LinearLayout llweixin = (LinearLayout) contentView.findViewById(R.id.ll_weixing);
        LinearLayout lllianjie = (LinearLayout) contentView.findViewById(R.id.ll_lianjie);
        LinearLayout llerweima = (LinearLayout) contentView.findViewById(R.id.ll_erweima);
        RelativeLayout rlbackground = (RelativeLayout) contentView.findViewById(R.id.rl_background);
        LinearLayout ll = (LinearLayout) contentView.findViewById(R.id.ll);
        rlbackground.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });
        ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        lllianjie.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (wxURL == null) {
                    LoadingDialogUtils.showProgress(BecomeShoperDetailsActivity.this, "正在加载,请稍后~");
                    wxmodel.getData(null, "3", LIANJIE, BecomeShoperDetailsActivity.this);
                } else {
                    ShareUtils.copyLianjie(BecomeShoperDetailsActivity.this, wxURL);
                }
            }
        });
        llweixin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (wxURL == null) {
                    LoadingDialogUtils.showProgress(BecomeShoperDetailsActivity.this, "正在加载,请稍后~");
                    wxmodel.getData(null, "3", WEIXINHAOYOU, BecomeShoperDetailsActivity.this);
                } else {
                    ShareUtils.wxshare( MyApp.loginMessage.getString("nickname", "") + "邀请您加入半职VIP，消费省钱，分享赚钱", MyApp.loginMessage.getString("personAvatar", ""), "全球十佳网商联合出品。匠心工厂，源头供货，正品优品，尽在半职。", wxURL);
                }
            }
        });
        llpengyouquan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (wxURL == null) {
                    LoadingDialogUtils.showProgress(BecomeShoperDetailsActivity.this, "正在加载,请稍后~");
                    wxmodel.getData(null, "3", PENGYOUQUAN, BecomeShoperDetailsActivity.this);
                } else {
                    ShareUtils.wxPengyouquan("半职", null, wxURL);
                }
            }
        });
        llerweima.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (wxURL == null) {
                    LoadingDialogUtils.showProgress(BecomeShoperDetailsActivity.this, "正在加载,请稍后~");
                    wxmodel.getData(null, "3", ERWEIMA, BecomeShoperDetailsActivity.this);
                } else {
                    // ShareUtils.ereimakaidian(BecomeShoperDetailsActivity.this, MyApp.loginMessage.getString("nickname", "") + "邀请您加入半职", "", null, null, null, MyApp.loginMessage.getString("shopimg", ""), wxURL);
                    ShareUtils.ereimakaidian(BecomeShoperDetailsActivity.this, MyApp.loginMessage.getString("nickname", "") + "邀请您加入半职", "", null, null, null, MyApp.loginMessage.getString("shopimg", ""), wxURL);
                    //  ShareUtils.ereima3(this, goodsShareDetailsData.getTitle(), goodsShareDetailsData.getMarketprice(), null, null, null, goodsShareDetailsData.getThumb_url().get(0), wxURL);
                }
            }
        });
        ivchacha.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                wxURL = null;
                popWindow.dismiss();
            }
        });
        ColorDrawable dw = new ColorDrawable(0x70000000);
        popWindow.setBackgroundDrawable(dw);
        popWindow.showAtLocation(contentView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    //详情数据

    @Override
    public void ongetDataSuccess(Object object) {
        goodsShareDetailsData = (GoodsShareDetails) object;

        showgoodsShareDetailsData();
        showWebView();
        mInviteShareSuCaiModel.getData(goodID,this);


    }

    @Override
    public void ongetDataError(Object object) {
        if (mSrlListView3.isRefreshing()) {
            mSrlListView3.setRefreshing(false);
        }
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
        LoadingDialogUtils.dismiss();

    }


    @Override
    public void ongetWXdataSuccess(Object object, int num) {
        LoadingDialogUtils.dismiss();
        WXshare share = (WXshare) object;
        wxURL = share.getRes();
        if (num == LIANJIE) {
            ShareUtils.copyLianjie(this, wxURL);
        } else if (num == ERWEIMA) {
            ShareUtils.ereima3(this, goodsShareDetailsData.getTitle(), goodsShareDetailsData.getMarketprice(), null, null, null, goodsShareDetailsData.getThumb_url().get(0), wxURL);
        } else if (num == WEIXINHAOYOU) {
            if (type == 1) {
                ShareUtils.wxshare(goodsShareDetailsData.getTitle(), goodsShareDetailsData.getThumb_url().get(0), "全球十佳网商联合出品。匠心工厂，源头供货，正品优品，尽在半职。", wxURL);
            } else if (type == 2) {
                ShareUtils.wxshare(inviteShareSuCaiData.get(position).getTitle(), inviteShareSuCaiData.get(position).getThumb_url().get(0), inviteShareSuCaiData.get(position).getDetail(), wxURL);
            }
        } else if (num == PENGYOUQUAN) {
            ShareUtils.wxPengyouquan(goodsShareDetailsData.getTitle(), goodsShareDetailsData.getThumb_url().get(0), wxURL);
        }
    }

    @Override
    public void ongetWXdataError(Object object, int num) {
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }

    /*
    * 展示邀请店长素材界面数据
    * */
    private void ShowInviteShareSuCaiData() {
        if (( mAdapter == null)) {
            mAdapter = new InviteShareSuCaiAdapter( this, inviteShareSuCaiData );
            mLvSuCai3.setAdapter( mAdapter );

        }else {
            mAdapter.notifyDataSetChanged();
        }
        mAdapter.setInviteShareCallBack(this);
        LoadingDialogUtils.dismiss();

    }



    @Override
    public void inviteShare(InviteShareSuCai_Data data, int position) {
        showSharePopupwindow(goodsShareDetailsData );
    }
    private List<InviteShareSuCai_Data> mInviteShareSuCai_datas;
    //获取素材成功
    @Override
    public void ongetInviteShareSuCaiDataSuccess(Object object) {
        if (mSrlListView3.isRefreshing()) {
            mSrlListView3.setRefreshing(false);
        }
        mInviteShareSuCai_datas = (List<InviteShareSuCai_Data>) object;
        inviteShareSuCaiData.clear();
        inviteShareSuCaiData.addAll( mInviteShareSuCai_datas);
        if (inviteShareSuCaiData.size() == 0) {
            mRlNoData.setVisibility(View.VISIBLE);
        } else {
            mRlNoData.setVisibility(View.GONE);
        }
        LoadingDialogUtils.dismiss();
        ShowInviteShareSuCaiData();
    }

    @Override
    public void ongetInviteShareSuCaiDataError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
        LoadingDialogUtils.dismiss();

    }

    @Override
    public void onRefresh() {
        LoadingDialogUtils.showProgress( this, "正在加载，请稍等" );
        mInviteShareSuCaiModel.getData( goodID,this );
    }
    public String getGoodID(){
        return goodID;
    }

    @Override
    public void gettext(int str, int position) {
        strs[position] = str;

        LogUtils.e("position:",position+"");

        if (strs != null) {
            for (int i = 0; i < strs.length; i++) {
                if (strs[i] == 0) {
                    isallcheck = false;
                    break;
                } else {
                    isallcheck = true;
                    continue;
                }
            }
        } else {
            isallcheck = true;
        }
        allguige.setLength(0);
        if (isallcheck) {
            int num = strs.length;
            int[] guigePx = new int[num];
            for (int i = 0; i < num; i++) {
                guigePx[i] = strs[i];
            }

            Arrays.sort(guigePx);
            for (int i = 0; i < goodsShareDetailsData.getOptionstit().size(); i++) {
                allguige.append(guigePx[i]);
         //       LogUtils.e("allguige1:",allguige.toString());
                if (i != goodsShareDetailsData.getOptionstit().size() - 1) {
                    allguige.append("_");
                }
            }

            guige = allguige.toString();
            for (int i = 0; i < goodsShareDetailsData.getOptions().size(); i++) {
                // LogUtils.e("guige2:",goodsShareDetailsData.getOptions().get(i).getSpecs());
                if (guige.equals(goodsShareDetailsData.getOptions().get(i).getSpecs())) {
                    guigejiage = goodsShareDetailsData.getOptions().get(i).getMarketprice();
                    guigekuchun = goodsShareDetailsData.getOptions().get(i).getStock()+"";
                    guigelirun = goodsShareDetailsData.getOptions().get(i).getProfit();

                    guigeid = goodsShareDetailsData.getOptions().get(i).getId()+"";
                    guigeimage = goodsShareDetailsData.getOptions().get(i).getThumb();
                    guigetitle = goodsShareDetailsData.getOptions().get(i).getTitle();
                }
            }
            if (ASUtils.isCanUse(guigejiage)) {
                tvguigeprice.setText(NumberUtils.DecimalFormat(guigejiage));
            }
            if (ASUtils.isCanUse(guigelirun)) {
                tvguigeliyun.setText(NumberUtils.DecimalFormat(guigelirun));
            }
            if (ASUtils.isCanUse(guigeimage)) {
                Picasso.with(this).load(guigeimage).placeholder(R.mipmap.zhanweitu2).into(ivguigeimage);
            } else {
                ivguigeimage.setImageResource(R.mipmap.zhanweitu2);
            }
            if (Integer.parseInt(guigekuchun) < 0) {
                tvguigekucun.setText("0");
            } else {
                tvguigekucun.setText(guigekuchun);
            }


        }
    }

    @Override
    public void ongetSucaiSuccess(Object object) {

    }

    @Override
    public void ongetSucaiError(Object object) {

    }

    @Override
    public void onaddCartSuccess(Object object) throws JSONException {

    }

    @Override
    public void onaddCartError(Object object) {

    }

    @Override
    public void onbuySuccess(Object object) {
        Buy_Data data = (Buy_Data) object;
        Intent intent = new Intent(this, ShareOrderAffirmActivity.class);
        intent.putExtra("goods_data", data);
        intent.putExtra("optionid", guigeid);
        intent.putExtra("goodsid", goodID);
        intent.putExtra("total", goodsnum);
        startActivity(intent);
    }

    @Override
    public void onbuyError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
        Intent intent = new Intent(this, AddAddressActivity.class);
        startActivity(intent);

    }
}
